package org.nch.videoLauncher.task;

import org.nch.videoLauncher.domain.*;
import org.nch.videoLauncher.repository.VideoItemRepository;
import org.nch.videoLauncher.repository.PlaybackEventRepository;
import org.nch.videoLauncher.util.IMDB;

import java.util.List;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Nov 14, 2008
 * Time: 9:37:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class VideoItemTask {

    private PlayerService playerService;

    public VideoItemTask() {

    }

    public VideoItem getVideoItem(Integer id) {

        VideoItemRepository vir = new VideoItemRepository();
        VideoItem vi = vir.getById(id);
        return vi;
    }

    public VideoItem saveVideoItem(VideoItem vi) {

        VideoItemRepository vir = new VideoItemRepository();
        vir.persist(vi);
        return vi;
    }

    public void deleteVideoItem(VideoItem vi) {

        VideoItemRepository vir = new VideoItemRepository();
        vir.delete(vi);
    }

    public List<VideoItem> getAllVideoItems() {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getAll();
    }

    public List<VideoItem> getAllMovies() {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getAllMovies();
    }

    public List<VideoItem> getAllTvShows() {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getAllTvShows();
    }

    public List<VideoItem> getAllSportsEvents() {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getAllSportsEvents();
    }

    public List<VideoItem> saveVideoItemList(List<VideoItem> videoItems) {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.persistAll(videoItems);
    }

    public void deleteVideoItemList(List<VideoItem> videoItems) {
        VideoItemRepository vir = new VideoItemRepository();
        for (VideoItem vi : videoItems) {
            vir.delete(vi);
        }
    }

    public void playVideoItem(VideoItem vi) {

        playerService = new PlayerService(ProfileUtil.getProfile());
        playerService.playVideoItem(vi);
        logPlaybackEvent(vi);
    }

    public void playVideoItemFromFile(String filename) {

        playerService = new PlayerService(ProfileUtil.getProfile());
        playerService.playVideoFromFile(filename);
    }

    public void openUrl(VideoItem vi) {

        playerService = new PlayerService(ProfileUtil.getProfile());
        playerService.openUrl(vi);

    }

    private void logPlaybackEvent(VideoItem vi) {

        PlaybackEventRepository pbr = new PlaybackEventRepository();
        PlaybackEvent pbe = new PlaybackEvent(vi, ProfileUtil.getProfile());
        pbr.persist(pbe);

    }

    public void loadIMDBData(VideoItem vi) {
        IMDBInfo imdbInfo = vi.getImdbInfo();
        String imdbKey = imdbInfo.getImdbKey();
        if (!(imdbKey.equals("") && imdbKey.equals(null))) {
            try {
                IMDB imdbCapture = new IMDB(imdbKey);
                imdbInfo.setAka(imdbCapture.getAka());
                imdbInfo.setCast(imdbCapture.getCast());
                imdbInfo.setCover(imdbCapture.getCover());
                imdbInfo.setCoverUrl(imdbCapture.getCoverURL());
                imdbInfo.setDate(imdbCapture.getDate());
                imdbInfo.setDirectedBy(imdbCapture.getDirectedBy());
                imdbInfo.setGenre(imdbCapture.getGenre());
                imdbInfo.setMpaa(imdbCapture.getMpaa());
                imdbInfo.setPlot(imdbCapture.getPlot());
                imdbInfo.setRating(imdbCapture.getRating());
                imdbInfo.setTitle(imdbCapture.getTitle());
                imdbInfo.setWrittenBy(imdbCapture.getWrittenBy());
                vi.setRunTime(imdbCapture.getRuntime());
                vi.setTitle(vi.getTitle());
                vi.setDescription(imdbInfo.getPlot());
                vi.setKeywords(imdbInfo.getGenre());
                vi.setUrl("http://www.imdb.com/title/tt" + imdbInfo.getImdbKey() + "/");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public List<PlaybackEvent> getHistoryForVideoItem(VideoItem vi) {

        PlaybackEventRepository pbr = new PlaybackEventRepository();
        return pbr.getForVideoItem(vi);

    }

    public List<PlaybackEvent> getAllHistory() {
        PlaybackEventRepository pbr = new PlaybackEventRepository();
        return pbr.getAll();
    }

    public List<VideoItem> getQuickSearchResults(String searchText) {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getQuickSearchResults(searchText);
    }

    public List<VideoItem> getMovieSearchResults(HashMap searchTerms) {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getMovieSearchResults(searchTerms);
    }

    public List<VideoItem> getTvSearchResults(HashMap searchTerms) {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getTvSearchResults(searchTerms);
    }

    public List<VideoItem> getSportsSearchResults(HashMap searchTerms) {
        VideoItemRepository vir = new VideoItemRepository();
        return vir.getSportsSearchResults(searchTerms);
    }
}
